package com.seirsoft.cookingtime.ui.dialogfactory;

import java.lang.reflect.Constructor;
import java.util.Hashtable;

import org.eclipse.jface.window.IShellProvider;
import org.eclipse.swt.widgets.Shell;

import com.seirsoft.cookingtime.view.IDlgBase;

public class DialogFactory {

	public IDlgBase getDialogDisplayTime(IDlgBase parenDialog) {
		return getNewDialog(parenDialog, EnumDialogType.enumDialogDisplayTime);
	}

	private IDlgBase getNewDialog(IDlgBase parenDialog, int enumNewDialogType) {
		if (parenDialog instanceof IShellProvider) {
			IShellProvider baseShell = (IShellProvider) parenDialog;
			if (baseShell != null) {

				String strClassName = m_hashDialogType_DialogClassName
						.get(enumNewDialogType);

				IDlgBase obj = null;
				try {
					Class<?> cls = Class.forName(strClassName);

					
					
					Class<?>[] parameterTypes = new Class[] { Shell.class };
					Constructor<?> constructor = cls.getConstructor(parameterTypes);

					if(constructor != null)
					{
						Object[] parameters = new Object[] { baseShell.getShell() };
						obj = (IDlgBase) constructor.newInstance(parameters);
					}

				} catch (ClassNotFoundException cnfe) {
					System.out.println("ClassNotFoundException:"
							+ cnfe.getMessage());

				} catch (Exception e) {
					e.printStackTrace();

				}

				return obj;
			}
			else
			{
				System.out.println("base dialog is not IShellProvider.");
			}
		}
		else
		{
			System.out.println("base dialog is not IShellProvider.");
		}

		return null;
	}

	

	public static DialogFactory getInstance() {
		if (DialogFactory.m_objDialogFactory == null) {
			synchronized (DialogFactory.m_initLock) {
				if (DialogFactory.m_objDialogFactory == null) {
					DialogFactory.m_objDialogFactory = new DialogFactory();
				}
			}
		}

		return DialogFactory.m_objDialogFactory;
	}

	private static Object m_initLock = new Object();
	private static DialogFactory m_objDialogFactory = null;

	private Hashtable<Integer, String> m_hashDialogType_DialogClassName = null;

	private interface EnumDialogType {
		final int enumBase = 0;
		// regist all Dialog
		final int enumDialogDisplayTime = enumBase + 1;
	}

	private DialogFactory() {
		this.m_hashDialogType_DialogClassName = new Hashtable<Integer, String>();

		// regist all Dialog
		this.m_hashDialogType_DialogClassName.put(
				EnumDialogType.enumDialogDisplayTime,
				"com.seirsoft.cookingtime.ui.dialog.DialogDisplayTime");
	}

}
